package com.pinecone.controller;

import com.pinecone.common.utils.R;
import com.pinecone.dto.CustomerRegisterDTO;
import com.pinecone.dto.UserLoginDTO;
import com.pinecone.dto.UserRegisterDTO;
import com.pinecone.entity.CusHostEntity;
import com.pinecone.entity.CusOrgInfoEntity;
import com.pinecone.entity.CusPrincipalEntity;
import com.pinecone.entity.SysUserEntity;
import com.pinecone.service.CusHostService;
import com.pinecone.service.CusOrgInfoService;
import com.pinecone.service.CusPrincipalService;
import com.pinecone.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 所有用户授权控制器
 * Created by hushangjie on 2017/9/28.
 */
@RestController
@RequestMapping("auth")
public class AuthController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private CusOrgInfoService orgInfoService;
    @Autowired
    private CusPrincipalService cusPrincipalService;
    @Autowired
    private CusHostService cusHostService;

    //系统用户注册
    @PostMapping("/sys/register")
    public R registerSysUser(@RequestBody @Validated UserRegisterDTO registerDTO){
        SysUserEntity sysUserEntity = new SysUserEntity();
        sysUserEntity.setUsername(registerDTO.getUsername())
                .setPassword(registerDTO.getPassword());
        sysUserService.registerSysUser(sysUserEntity,registerDTO.getRoleId());
        return R.ok();
    }
    //系统用户登录
    @PostMapping("/sys/login")
    public R sysUserlogin(@RequestBody @Validated UserLoginDTO userLoginDTO){
        String token = sysUserService.login(userLoginDTO.getUsername(),userLoginDTO.getPassword());
        return R.ok().put("token",token);
    }
    //机构管理员注册
    @PostMapping("/cus/register")
    public R registerCusMaster(@RequestBody @Validated CustomerRegisterDTO customerRegisterDTO){
        //验证图片验证码
        //verificationService.validateCodeNum(request.getSession().getId(),customerRegisterDTO.getImgCode());
        //验证短信验证码
        //verificationService.validateSMSNum(customerRegisterDTO.getUsername(),customerRegisterDTO.getSmsCode());
        //保存机构信息
        CusOrgInfoEntity cusOrgInfoEntity = new CusOrgInfoEntity();
        cusOrgInfoEntity.setCName(customerRegisterDTO.getOrgPrincipalName())
                .setCPhone(customerRegisterDTO.getUsername())
                .setOrgAddr(customerRegisterDTO.getOrgAddr())
                .setOrgName(customerRegisterDTO.getOrgName());
        orgInfoService.save(cusOrgInfoEntity);
        //保存用户信息
        CusPrincipalEntity principalEntity = new CusPrincipalEntity();
        principalEntity.setCusAccount(customerRegisterDTO.getUsername())
                .setCusPassword(customerRegisterDTO.getPassword())
                .setLastPasswordResetDate(new Date())
                .setOrgId(cusOrgInfoEntity.getOrgId())
                .setParent(true)
                .setState(true);
        cusPrincipalService.save(principalEntity);
        //将该机构管理员设置为主播身份
        CusHostEntity hostEntity = new CusHostEntity();
        hostEntity.setCustomerId(principalEntity.getCusId())
                .setHostName(customerRegisterDTO.getOrgPrincipalName())
                .setHostPhone(customerRegisterDTO.getUsername())
                .setOrgId(cusOrgInfoEntity.getOrgId())
                .setRemark("您是机构最高级管理员，拥有最高级权限");
        cusHostService.save(hostEntity);

        return R.ok();
    }
    //机构管理员和主播登录
    @PostMapping("/cus/login")
    public R cusLogin(@RequestBody @Validated UserLoginDTO userLoginDTO){
        String token = cusPrincipalService.login(userLoginDTO.getUsername(),userLoginDTO.getPassword());
        CusPrincipalEntity principalEntity = cusPrincipalService.queryByAccount(userLoginDTO.getUsername());
        CusHostEntity hostEntity = cusHostService.queryByCusId(principalEntity.getCusId());
        Map<String,Object> userInfo = new HashMap<>();
        userInfo.put("token",token);
        userInfo.put("username",principalEntity.getCusAccount());
        userInfo.put("avatar",hostEntity.getHostAvatar());
        userInfo.put("orgId",principalEntity.getOrgId());
        return R.ok().put("userInfo",userInfo);
    }




}
